//
//  HZLeftViewController.m
//  HZTC_Stock
//
//  Created by MayerF on 2017/5/24.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZLeftViewController.h"
#import "HZTabBarController.h"
#import "HZLeftView.h"

@interface HZLeftViewController ()
@property (nonatomic, strong)HZLeftView *pLeftView;
@property (nonatomic, strong)HZTabBarController *pTabBarVC;
@property (nonatomic, assign)CGFloat fSlideCenterXDistace; //中心点侧滑距离
@end

@implementation HZLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _pLeftView = [HZLeftView new];
    [self.view addSubview:_pLeftView];
    [_pLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view).offset(-(CGRectGetWidth(self.view.frame)/2 - 64));
        make.bottom.mas_equalTo(self.view);
    }];
    
    //注册block
    __weak typeof (self)weakSelf = self;
    [[MYRouter shared] map:kRouterBlockLeftViewList toBlock:^id(NSDictionary *params, id other) {
        [weakSelf userInfoOnClickWithController:other];
        return nil;
    }];
    
    
    HZTabBarController *tabBarVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"tabBar"];
    [self addChildViewController:tabBarVC];
    tabBarVC.view.frame = self.view.bounds;
    [self.view addSubview:tabBarVC.view];
    [tabBarVC didMoveToParentViewController:self];
    _pTabBarVC = tabBarVC;
    
    _fSlideCenterXDistace = 64 + CGRectGetWidth(self.view.frame);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLeftController) name:kShowLeftController object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hiddenLeftController) name:kHiddenLeftController object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gradualHiddenLeftController:) name:kGradualHiddenLeftController object:nil];
}

#pragma mark -
#pragma mark action
- (void)userInfoOnClickWithController:(UIViewController *)controller
{
    [self hiddenLeftController];
    UINavigationController *currentNavVC = _pTabBarVC.selectedViewController;
    [currentNavVC pushViewController:controller animated:YES];
}

#pragma mark -
#pragma mark NSNotification
- (void)showLeftController
{
    [UIView animateWithDuration:0.25 animations:^{
        _pTabBarVC.view.center = CGPointMake((UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) ? _fSlideCenterXDistace : _fSlideCenterXDistace), [UIApplication sharedApplication].keyWindow.center.y);
        _pTabBarVC.pAlphaView.hidden = NO;
        _pTabBarVC.pAlphaView.alpha = 0.5;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hiddenLeftController
{
    [UIView animateWithDuration:0.25 animations:^{
        _pTabBarVC.view.center = CGPointMake([UIApplication sharedApplication].keyWindow.center.x, [UIApplication sharedApplication].keyWindow.center.y);
        _pTabBarVC.pAlphaView.alpha = 0.f;
    } completion:^(BOOL finished) {
        _pTabBarVC.pAlphaView.hidden = YES;
    }];
}
- (void)gradualHiddenLeftController:(NSNotification *)noti
{
    CGFloat distance = [[noti.object valueForKey:@"distance"] floatValue];
    UIGestureRecognizerState nState = [[noti.object valueForKey:@"state"] integerValue];
    CGFloat fWindowCenterX = [UIApplication sharedApplication].keyWindow.center.x;
    CGFloat currentCenterX = _pTabBarVC.view.center.x - distance;
    if (currentCenterX < fWindowCenterX) {
        currentCenterX = fWindowCenterX;
    }else if (currentCenterX > _fSlideCenterXDistace) {
        currentCenterX = _fSlideCenterXDistace;
    }
    
    if (nState == UIGestureRecognizerStateEnded) {
        if (currentCenterX < ((_fSlideCenterXDistace - fWindowCenterX)/2 + fWindowCenterX)) {
            currentCenterX = fWindowCenterX;
        }else if (currentCenterX >= ((_fSlideCenterXDistace - fWindowCenterX)/2 + fWindowCenterX)) {
            currentCenterX = _fSlideCenterXDistace;
        }
    }
    _pTabBarVC.view.center = CGPointMake(currentCenterX, [UIApplication sharedApplication].keyWindow.center.y);
    
    CGFloat fAlpha = (currentCenterX - fWindowCenterX)/(_fSlideCenterXDistace - fWindowCenterX)*0.5;
    _pTabBarVC.pAlphaView.alpha = fAlpha;
    if (currentCenterX == fWindowCenterX) {
        _pTabBarVC.pAlphaView.hidden = YES;
    }else {
        _pTabBarVC.pAlphaView.hidden = NO;
    }
}

@end
